package main.role;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;

import main.staff.CreateTypesSql;
import main.staff.MenuAndAuth;
import main.util.CellRangeAddressUtil;
import main.util.JdbcUtil;
import main.util.ReplaceSQLString;
import main.util.SqlConstants;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


public class CreateSqlNOID {

	public static FileWriter auth,button;
	
	public static JdbcUtil db=new JdbcUtil();
	
	public static CellRangeAddressUtil cau;
	
	public static Sheet sheet;
	
	public static int endCol=2;
	
	public static void main(String[] args) {
		InputStream inp;
		try {
			inp = new FileInputStream("e:/柳州新工作流系统使用人员信息收集.xlsx");
			
			Workbook wb = WorkbookFactory.create(inp);
			sheet = wb.getSheetAt(1);
			endCol=3;//按钮的时候为3

			auth= new FileWriter("e:/角色权限t_ucp_roleauthment.sql");
			button= new FileWriter("e:/角色权限(按钮)t_ucp_roleauthment.sql");
			 cau=new CellRangeAddressUtil(sheet);
			 SqlConstants.topWriteHead(auth);
			 SqlConstants.topWriteHead(button);
			 
			 
			 operateAuthRow(sheet);//菜单
			 
			 SqlConstants.topWriteEnd(auth);
			 SqlConstants.topWriteEnd(button);
			 
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			db.close();
			try {
				auth.close();
				button.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void operateAuthRow(Sheet sheet) throws IOException{
		NewRoleAuth newRoleAuth=new NewRoleAuth(sheet, 4, 7, 0);
		for(int i=0;i<sheet.getLastRowNum()+1;i++){
			Row row = sheet.getRow(i);
			
			String authName=SqlConstants.getColValue(row, 0);
			String authId=NewRoleAuth.getAuthIdByParentId("000", authName, db);
			if(authId==null){
				continue;
			}
			
			newRoleAuth.setModule(authId.substring(3,authId.length()));
			
			i=operateAuthCol(0, row, "000", newRoleAuth);
		}
	}
	
	public static int operateAuthCol(int col,Row row,String superCode,NewRoleAuth newRoleAuth) throws IOException{
		Integer startRow=row.getRowNum();
		Integer endRow=cau.getMergeRow(row.getCell(col));
		
		if(col>endCol){
			return startRow;
		}
		
		if(endRow==null){
			endRow=startRow;
		}
		String authName=SqlConstants.getColValue(row, col);
		String authId=NewRoleAuth.getAuthIdByParentId(superCode, authName, db);
		if(authId==null && col>2){
			System.out.print(newRoleAuth.getRoleSql(authId, startRow, endRow));
			return endRow;
		}

		String sql=newRoleAuth.getRoleSql(authId, startRow, endRow);
		if(col>2){
			button.write(sql);
		}else{
			auth.write(sql);
		}
		
		for(;startRow<endRow+1;startRow++){
			int nextCol=col+1;
			startRow=operateAuthCol(nextCol, sheet.getRow(startRow),authId, newRoleAuth);//行的循环，列的递归循环，
		}
		
		return endRow;
		
	}
	
	
	
	public static void operateAuthRole(Sheet sheet){
		try {
			for(int i=0;i<sheet.getLastRowNum()+1;i++){
				Row row = sheet.getRow(i);
				String authId=SqlConstants.getColValue(row, 5);
				
				if(authId.matches("[0-9]{14,}+")){
					//staffInfo.write(CreateTypesSql.staffbasicinfo(row,staffid));
					auth.write(RoleAuthelement.getRoleSql(row, authId, 6, 16));
					//serviceType.write(CreateTypesSql.staffserviceType(row,staffid));
					//password.write(CreateTypesSql.password(staffid));
					System.out.println(authId+":成功   "+i);
				}else{
					System.out.println(authId+":loss  "+i);
				}
			}
		} catch (IOException e) {
			System.out.println("'''''''''''''''''''''''''"+sheet.getSheetName()+"添加失败");
			e.printStackTrace();
		}
	}
	
}
